package org.xxpay.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.xxpay.core.entity.AlipayCashDetail;
import org.xxpay.core.entity.AlipayCashDetailExample;
import org.xxpay.core.entity.PayInterfaceTypeTemplate;
import org.xxpay.core.service.IAlipayCashDetailService;
import org.xxpay.service.dao.mapper.AlipayCashDetailMapper;
import org.xxpay.service.dao.mapper.AlipayCashUserMapper;
import org.xxpay.service.dao.mapper.PayInterfaceTypeTemplateMapper;

import java.util.List;
import java.util.Map;

@Service
public class AlipayCashDetailServiceImpl implements IAlipayCashDetailService {

    @Autowired
    private AlipayCashDetailMapper recordMapper;

    @Autowired
    private AlipayCashUserMapper alipayCashUserMapper;

    @Autowired
    private PayInterfaceTypeTemplateMapper payInterfaceTypeTemplateMapper;

    @Override
    public List<AlipayCashDetail> select(int pageIndex, int pageSize, AlipayCashDetail record) {

        AlipayCashDetailExample exa = getFullExample(record);
        exa.setOrderByClause("CreateTime desc");
        exa.setOffset(pageIndex);
        exa.setLimit(pageSize);
        return recordMapper.selectByExample(exa);
    }

    @Override
    public int count(AlipayCashDetail record) {
        return recordMapper.countByExample(getFullExample(record));
    }

    @Override
    public AlipayCashDetail findById(Long od) {
        return null;
    }

    @Override
    public int add(AlipayCashDetail record) {
        return 0;
    }

    @Override
    public int update(AlipayCashDetail record) {
        return 0;
    }

    @Override
    @Transactional(transactionManager = "transactionManager", rollbackFor = Exception.class)
    public void updateDetailAndUser(AlipayCashDetail updateRecord, Integer cashId, Integer alipayCashUserId) {

        recordMapper.updateByPrimaryKeySelective(updateRecord); //更新数据
        if(updateRecord.getStatus() == AlipayCashDetail.STATUS_SUCCESS){ //分账成功
            alipayCashUserMapper.addTotalMoneyAndState(alipayCashUserId, updateRecord.getRealAmount());

            //累加分账总金额
            PayInterfaceTypeTemplate updateTemplateRecord = new PayInterfaceTypeTemplate();
            updateTemplateRecord.setTotalCashAmount(updateRecord.getRealAmount());
            updateTemplateRecord.setPsVal("alipayCashId", cashId);
            payInterfaceTypeTemplateMapper.addTotalAmount(updateTemplateRecord);
        }
    }

    @Override
    public List<AlipayCashDetail> selectByCashId(Integer cashId) {
        AlipayCashDetailExample exa = new AlipayCashDetailExample();
        exa.createCriteria().andCashIdEqualTo(cashId);
        return recordMapper.selectByExample(exa);
    }


    @Override
    public Map<String, Long> selectTotalCashAmount(AlipayCashDetail record){
        return recordMapper.selectTotalCashAmount(record);
    }


    private AlipayCashDetailExample getFullExample(AlipayCashDetail record){

        if(record == null) return new AlipayCashDetailExample();
        AlipayCashDetailExample exa = new AlipayCashDetailExample();
        AlipayCashDetailExample.Criteria c = exa.createCriteria();
        if(record.getId() != null) c.andIdEqualTo(record.getId());
        if (record.getCashId() != null) c.andCashIdEqualTo(record.getCashId());
        if(record.getBelongUserId() != null) c.andBelongUserIdEqualTo(record.getBelongUserId());
        return exa;
    }













}
